package com.yueka.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yueka.mapper.RoleMapper;
import com.yueka.pojo.PageResult;
import com.yueka.pojo.role.Role;
import com.yueka.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> list(String keyword, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Role> list = roleMapper.list(keyword);
        Page<Role> p = (Page<Role>) list;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public Integer delete(Integer[] ids) {
        return roleMapper.delete(ids);
    }

    @Override
    public Integer update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        if(role.getId() == null){
            role.setCreateTime(LocalDateTime.now());
            return roleMapper.insert(role);
        }
        return roleMapper.update(role);
    }

    @Override
    public List<Role> getAll() {
        return roleMapper.getAll();
    }


}
